<template>
  <div class="onekit-button"
       :class="onekitClass"
       :style="onekitStyle"
       @click.prevent="button_click"
       :loading="loading"
       :mini="mini"
       :disabled="disabled"
       :plain="plain"
       :type="type">
    <div class="loading"
         v-if="_loading"></div>
    <slot></slot>
  </div>
</template>

<script>
import weixin_behavior from "../../behaviors/weixin_behavior";
import onekit_behavior from "../../behaviors/onekit_behavior";
import eventBus from "../../api/EventBus";
export default {
  mixins: [weixin_behavior, onekit_behavior],
  data () {
    return {
      clicking: false,
      formData: {},
    };
  },
  name: "onekit-button",
  props: {
    type: {
      type: String,
      required: false,
    },
    disabled: {
      type: [Boolean, String],
      default: false,
      required: false,
    },
    mini: {
      type: [Boolean, String],
      default: false,
      required: false,
    },
    plain: {
      type: [Boolean, String],
      default: false,
      required: false,
    },
    loading: [Boolean, String],
    "form-type": String,
    "open-type": String,
  },
  methods: {
    button_click ($event) {
      if (this.disabled) {
        return
        }
      this.$emit("Tap", $event);
        if (this.formType === "submit") {
          const { changedTouches, currentTarget, target, timeStamp, touches } =
            $event;
          const detail = {
            formId: this.onekitId,
            target: {
              id: "",
              dataset: {},
              offsetLeft: "",
              offsetTop: "",
            },
            value: this.formData,
          };
          const type = "submit";
          const data = {
            changedTouches,
            currentTarget,
            target,
            detail,
            timeStamp,
            touches,
            type,
          };

          eventBus.$emit("onekit-form-submit-click", { data, $event });
        }
        if (this.formType === "reset") {
          const { changedTouches, currentTarget, target, timeStamp, touches } =
            $event;
          const detail = {
            target: {
              id: "",
              dataset: {},
              offsetLeft: "",
              offsetTop: "",
            },
          };
          const type = "reset";
          const data = {
            changedTouches,
            currentTarget,
            target,
            detail,
            timeStamp,
            touches,
            type,
          };
          eventBus.$emit("onekit-form-reset", { data, $event });
        }

        const telNum = "h5 is not supprot getting the cell phone number";
        this.$emit("Getphonenumber", telNum);
        const userInfo = {
          errMsg: "getuserinfo 即将开放",
        };
        this.$emit("Getuserinfo", userInfo);

        if (this.openType) {
          console.log("opentype 即将开放");
        }
      }
  },
  computed: {
    _loading () {
      let loading = false;
      if (this.loading) {
        loading = true;
      }
      return loading;
    },
  },
  mounted () {
    eventBus.$on("onekit-form-submit", (data) => {
      this.formData = data;
      // console.log(data)
    });
  },
};
</script>

<style>
</style>
